package com.gary.entity;

import java.io.Serializable;

public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final int CODE_SUCCESS = 0;
    private static final int CODE_ERROR = -1;
    private static final String MSG_SUCCESS = "success";
    private int code;
    private String msg;
    private T data;
    private String seq;

    public ApiResult() {
        this.code = 0;
        this.msg = "success";
    }

    public ApiResult(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    public ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ApiResult success() {
        return new ApiResult(0, "success");
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult(data);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult(0, msg, data);
    }

    public static ApiResult fail(String errorMsg) {
        return new ApiResult(-1, errorMsg);
    }


    public static ApiResult fail(int code, String errorMsg) {
        return new ApiResult(code, errorMsg);
    }

    public static ApiResult fail(Exception e) {
        return new ApiResult(-1, e == null ? "未知错误" : e.getMessage());
    }

    public static ApiResult error(String errorMsg) {
        return new ApiResult(-1, errorMsg);
    }

    public static ApiResult error(int code, String errorMsg) {
        return new ApiResult(code, errorMsg);
    }

    public static ApiResult error(Exception e) {
        return new ApiResult(-1, e == null ? "未知错误" : e.getMessage());
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getSeq() {
        return this.seq;
    }

    public void setSeq(String seq) {
        this.seq = seq;
    }
}